package com.uphone.kingmall.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.uphone.kingmall.activity.order.OrderDetailActivity;
import com.uphone.kingmall.adapter.MessageNotifactionListAdpater;
import com.uphone.kingmall.base.BaseFragment;
import com.uphone.kingmall.bean.MessageBean;
import com.uphone.kingmall.listener.NetListCallBack;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * author : SuMeng
 * e-mail : 986335838@qq.com
 * time   : 2019/02/21
 * desc   : 消息fragment
 */
public class MessageListFragment extends BaseFragment {
    @BindView(R.id.rv_message)
    RecyclerView rvMessage;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private int page = 1;
    private MessageNotifactionListAdpater messageNotifactionListAdpater;
    private List<MessageBean.DataBean> messageDataList = new ArrayList<>();

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_fragment_message;
    }

    public static MessageListFragment newInstance() {

        Bundle args = new Bundle();

        MessageListFragment fragment = new MessageListFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void initView(View view) {


        messageNotifactionListAdpater = new MessageNotifactionListAdpater();
        rvMessage.addItemDecoration(new RecycleViewDivider(
                getActivity(), LinearLayoutManager.VERTICAL, 1, getResources().getColor(R.color.colorTransparent)));
        rvMessage.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvMessage.setAdapter(messageNotifactionListAdpater);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                loadData(1);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                loadData(++page);
            }
        });

        messageNotifactionListAdpater.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MessageBean.DataBean dataBean = messageDataList.get(position);
//                1订单，2充值提现，3分红，4审核，5投诉
                switch (dataBean.getMsgMode()) {
                    case 1:
                        CommonUtil.startIntent(getActivity(), OrderDetailActivity.class, Integer.parseInt(dataBean.getTag()));
                        break;
                    case 2:
//                        openActivity(SmallChargeActivity.class);
                        break;
                    case 3:

                        break;
                    case 4:
//                        openActivity(ShangJiaRuZhuSuccessActivity.class);
                        break;
                    case 5:
                        break;
                }
            }
        });
    }

    @Override
    public void initData() {

//        if (position == 0) {
//            MessageListAdpater messageListAdpater = new MessageListAdpater();
//            rvMessage.addItemDecoration(new RecycleViewDivider(
//                    getActivity(), LinearLayoutManager.VERTICAL, 1, getResources().getColor(R.color.colorTransparent)));
//            rvMessage.setLayoutManager(new LinearLayoutManager(getActivity()));
//            rvMessage.setAdapter(messageListAdpater);
//
//        } else {
//        }
        loadData(1);
    }

    private void loadData(int page) {
        this.page = page;
        // 通知消息
        HttpParams params = new HttpParams();
        params.put("page", page);
        params.put("limit", "10");
        CommonUtil.netRv(new MessageBean(), messageNotifactionListAdpater, messageDataList, MyUrl.userMsgs, params, page, new NetListCallBack<MessageBean.DataBean>() {
            @Override
            public void callBack(List<MessageBean.DataBean> list) {
                messageDataList = list;
            }
        });
    }
}
